package com.kayang.wechat.controller;

import com.kayang.common.HttpMsg;
import com.kayang.wechat.config.WeChatConfig;
import com.kayang.wechat.service.AccessTokenService;
import com.kayang.wechat.pojo.UserModel;
import com.kayang.wechat.pojo.WechatContext;
import com.kayang.wechat.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseController{

    @Autowired
    UserService userService;

    @Autowired
    AccessTokenService tokenService;

    @Autowired
    WeChatConfig weChatConfig;

    @PostMapping("/creat")
    public ResponseEntity creatUser(@Valid  @RequestBody UserModel userModel){
        String url = weChatConfig.getPath() + "/cgi-bin/user/create?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN", tokenService.getABAccessTokenStr(weChatConfig.getAppKey()));
        WechatContext context = new WechatContext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(userModel.toMap(userModel));
        Map ret = userService.creatUser(userModel, context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("创建成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    @PostMapping("/update")
    public ResponseEntity updateUser(@Valid  @RequestBody UserModel userModel){
        String url = weChatConfig.getPath() + "/cgi-bin/user/update?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN", tokenService.getABAccessTokenStr(weChatConfig.getAppKey()));
        WechatContext context = new WechatContext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(userModel.toMap(userModel));
        Map ret = userService.updateUser(userModel, context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("修改成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    @GetMapping("/delete")
    public ResponseEntity deleteUser(@RequestParam String userid){
        String url = weChatConfig.getPath() + "/cgi-bin/user/delete?access_token=ACCESS_TOKEN&userid=USERID";
        url = url.replace("ACCESS_TOKEN", tokenService.getABAccessTokenStr(weChatConfig.getAppKey()));
        url = url.replace("USERID", userid);
        WechatContext context = new WechatContext();
        context.setUrl(url);
        context.setMethod("GET");
        Map ret = userService.deleteUser(context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("删除成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    /**
     * {
     *    "useridlist": ["zhangsan", "lisi"]
     * }
     * */
    @PostMapping("/batchdelete")
    public ResponseEntity batchDeleteUser(@Valid  @RequestBody Map useridmap){
        String url = weChatConfig.getPath() + "/cgi-bin/user/batchdelete?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN", tokenService.getABAccessTokenStr(weChatConfig.getAppKey()));
        WechatContext context = new WechatContext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(useridmap);
        Map ret = userService.batchdeleteUser(useridmap, context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("批量删除成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    @GetMapping("/get")
    public ResponseEntity getUser(@RequestParam String userid){
        String url = weChatConfig.getPath() + "/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=USERID";
        url = url.replace("ACCESS_TOKEN", tokenService.getABAccessTokenStr(weChatConfig.getAppKey()));
        url = url.replace("USERID", userid);
        WechatContext context = new WechatContext();
        context.setUrl(url);
        context.setMethod("GET");
        Map ret = userService.getUser(context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("查看成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

}
